package mysql

import "gorm.io/gorm"

type ClassOrder struct {
	gorm.Model
	ClassId   int     `gorm:"type:int;comment:课程id"`
	Name      string  `gorm:"type:varchar(20);comment:课程名称"`
	Dates     string  `gorm:"type:varchar(20);comment:日期"`
	Times     string  `gorm:"type:varchar(20);comment:时间"`
	Address   string  `gorm:"type:varchar(20);comment:场地地址"`
	Num       int     `gorm:"type:int;comment:成团人数"`
	Price     float32 `gorm:"type:decimal(10,2);comment:课程价格"`
	Status    int     `gorm:"type:int;default:1;comment:预约状态:1-正常上课，2-取消"`
	WritersId int     `gorm:"type:int;default:1;comment:确认信息:1-未确认，2-确认"`
	Writers   string  `gorm:"type:varchar(50);comment:用户备注信息"`
}

func (ClassOrder) TableName() string {
	return "class_order"
}

func (o *ClassOrder) AddClassOrder() error {
	return DB.Create(&o).Error
}

func (o *ClassOrder) UpdatedWritersId(Id int) error {
	return DB.Where("id=?", Id).Find(&o).Update("writers_id", 2).Error
}

func (o *ClassOrder) UpdatedWriters(writers string, Id int) error {
	return DB.Where("id=?", Id).Find(&o).Update("writers", writers).Error
}

func (o *ClassOrder) Finds(status int) (result []ClassOrder, err error) {
	err = DB.Where("status=?", status).Find(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}

func (o *ClassOrder) Find(id int) (result ClassOrder, err error) {
	err = DB.Where("id=?", id).Find(&result).Error
	if err != nil {
		return ClassOrder{}, err
	}
	return result, nil
}

func (o *ClassOrder) UpdatedStatus(id int) error {
	return DB.Where("id=?", id).Find(&o).Update("status", 2).Error
}
